#
# Common.mk
#
# Common definitions shared by several makefiles.
#

#
# Variables.
#

#
# OS variable.
#
# Variable that determines which platform are we running on.
#
OS := $(shell sh -c 'uname -s 2>/dev/null')

# Directory variables.

#
# BIN_DIR
#
# Defines where the bin directory is located.
#
BIN_DIR = bin

#
# DIST_DIR
#
# Defines where the dist directory is located.
#
DIST_DIR = dist

#
# DOC_DIR
#
# Defines where the doc directory is located.
#
DOC_DIR = doc

#
# INC_DIR
#
# Defines where the include directory is located.
#
INC_DIR = include

#
# LIB_DIR
#
# Defines where the lib directory is located.
LIB_DIR = lib

#
# OBJ_DIR
#
# Defines where the obj directory is located.
#
OBJ_DIR = obj

#
# SRC_DIR
#
# Defines where the src directory is located.
#
SRC_DIR = src

#
# CPP_SRC
#
# Defines where the C++ source is located.
#
CPP_SRC_DIR = $(SRC_DIR)/cpp

#
# LATEX_SRC
#
# Defines where the LaTeX source is located.
#
LATEX_SRC = $(SRC_DIR)/latex

#
# DIST_PREFIX
#
# Defines the value to be used for --prefix
# configuration parameter while building the distributions.
#
DIST_PREFIX = $(CURDIR)/

#
# DIST_LIBDIR
#
# Defines the value to be used for --libdir
# configuration parameter while building the distributions.
#
DIST_LIBDIR = $(DIST_PREFIX)$(LIB_DIR)


#
# Functions.
#

#
# curl function.
#
# Creates a command to download a file using curl.
#
# Arguments:
#	$(1)	-- output filename.
#	$(2)	-- URL to download.
#
curl = curl -L -o $(1) $(2)

#
# wget function.
#
# Creates a command to download a file using wget.
#
# Arguments:
#	$(1)	-- output filename.
#	$(2)	-- URL to download.
#
wget = wget -O $(1) $(2)

#
# download_file function.
#
# Creates a command to download a file using either
# curl or wget depending on which platform we are running.
#
# Arguments:
#	$(1)	-- output filename.
#	$(2)	-- URL to download.
#
download_file =						\
	$(if $(findstring Darwin,$(OS)),		\
		$(call curl,$(1),$(2)),			\
		$(call wget,$(1),$(2)))

#
# open function.
#
# Creates a command to open a file from the terminal.
#
open = $(if $(findstring Darwin,$(OS)),open,gnome-open)

#
# shared_library_name function.
#
# Returns the OS dependant shared library name for a
# given name. For example, .dylib in OS X and .so in Linux.
#
# Arguments:
#	$(1)	-- base library name.
#
shared_library_name = 							\
	$(if $(findstring Darwin,$(OS)), 				\
		$(call darwin_shared_library_name,$(1),$(2),$(3),$(4)),	\
		$(call linux_shared_library_name,$(1),$(2),$(3),$(4)))

#
# darwin_shared_library_name function.
#
# Returns the OS X specific shared library name for
# the base library name and the specified version numbers.
#
# Arguments:
#	$(1)	-- base library name.
#	$(2)	-- library major version number.
#	$(3)	-- library minor version number.
#	$(4)	-- library revision version number.
#
darwin_shared_library_name = 						\
	$(1)$(if $(2),.$(2))$(if $(3),.$(3))$(if $(4),.$(4)).dylib

#
# linux_shared_library_name function.
#
# Returns the Linux specific shared library name for
# the base library name and the specified version numbers.
#
# Arguments:
#	$(1)	-- base library name.
#	$(2)	-- library major version number.
#	$(3)	-- library minor version number.
#	$(4)	-- library revision version number.
#
linux_shared_library_name = 						\
	$(1).so$(if $(2),.$(2))$(if $(3),.$(3))$(if $(4),.$(4))

#
# tar_extract function.
#
# Extracts the content of the specified file to the specified directory.
#
# Arguments:
#	$(1)	-- input filename.
#	$(2)	-- output directory.
#
tar_extract = tar xzmpf $(1) --directory $(2)

#
# touch_recursive function.
#
# Touches all files and directories in the specified directory.
#
# Arguments:
#	$(1)	-- directory.
#
touch_recursive = find $(1) -exec touch {} \;


#
# Targets.
#

#
# Create bin directory.
#
$(BIN_DIR):
	@echo Creating $(BIN_DIR) directory...
	@mkdir -p $(BIN_DIR)

#
# Create distributions directory.
#
$(DIST_DIR):
	@echo Creating $(DIST_DIR) directory...
	@mkdir -p $(DIST_DIR)

#
# Create doc directory.
#
$(DOC_DIR):
	@echo Creating $(DOC_DIR) directory...
	@mkdir -p $(DOC_DIR)

#
# Create obj directory.
#
$(OBJ_DIR):
	@echo Creating $(OBJ_DIR) directory...
	@mkdir -p $(OBJ_DIR)
